package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.OfflineDetectionData;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.OfflineDetectionInterface;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2015/12/31.
 */
public class OfflineDetectionPresenter extends BasePresenter<OfflineDetectionInterface> {

    private DataManager dataManager;
    private OfflineDetectionInterface mView;
    private Subscription mSubscription;
    private int pageIndex = 1;

    public OfflineDetectionPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }
    public void resetPageIndex(){
        pageIndex = 1;
    }

    @Override
    public void attachView(OfflineDetectionInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }
    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /***
     * 认领审核
     * @param userid
     * @param taskid
     * @param key
     * @param value
     */
    public void claimTask(String userid,final String taskid,String key,String value,final int op){
        checkViewAttached();
        if (AppContext.isNetWork){
            Map<String,String> params = new HashMap<>();
            params.put(key, value);
            params.put("taskid", taskid);
            params.put("userId", userid);
            if(op==-1){
                params.put("qiangdan", "1");
            }
            mSubscription = dataManager.claimTask(params).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<SuccessData>() {
                @Override
                public void onCompleted() {
                    mView.dismissDialog();
                }
                @Override
                public void onError(Throwable e) {
                    if (e != null&&mView!=null) {
                        mView.showError(e.getMessage());
                    } else {
                        mView.showError(Constant.errr_net);
                    }
                }

                @Override
                public void onNext(SuccessData successData) {
                    if(successData.isSucceed()){
                        mView.claimSucceed();
                    }else{
                        String qiangdan = successData.getQiangdan();
                        if("1".equals(qiangdan)){
                            mView.qiangdan(taskid,successData.getMsg());
                        }else{
                            mView.claimFailed(successData.getMsg());
                        }
                    }
                }
            });
        }
    }
    /***
     * 获取线下任务列表
     * @param userID
     * @param datetype
     * @param status
     * @param Operation
     * @param PageId
     */
    public void loadOffineDataNew(String userID, String datetype, String status, String Operation, String PageId){
        checkViewAttached();
        mSubscription = dataManager.loadOfflineDetectionNew(pageIndex,userID,datetype,status,Operation,PageId).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<OfflineDetectionData>() {
            @Override
            public void onCompleted() { }
            @Override
            public void onError(Throwable e) {
                if(e!=null){
                    mView.showError(e.getMessage());
                }
            }
            @Override
            public void onNext(OfflineDetectionData offlineDetectionData) {
                Logger.e(offlineDetectionData.toString());
                if(offlineDetectionData.getStatus()==100){
                    if(pageIndex==1){
                        mView.showDatas(offlineDetectionData);
                    }else{
                        mView.showMoreDatas(offlineDetectionData);
                    }

                }else{
                    mView.showError(offlineDetectionData.getMsg());
                }
                List<OfflineDetectionData.OffLineTaskListEntity> offLineTaskList = offlineDetectionData.getOffLineTaskList();
//                for (OfflineDetectionData.OffLineTaskListEntity item:offLineTaskList  ) {
//                    Log.e("pk","message operation:"+item.getMessageOperation()+",chejiaohao:"+item.getVin());
//                }
                if(offLineTaskList!=null && offLineTaskList.size()>0){
                    pageIndex++;
                }
            }
        });
    }

}
